import 'package:ad_common/third_ad.dart';
import 'package:ad_common/third_ad_instance.dart';
import 'package:flutter/foundation.dart';

import 'ad_container.dart';
import 'ad_error.dart';
import 'ad_listener.dart';
import 'ad_manager.dart';
import 'ad_source.dart';

/// FileName native_ad
/// @Author zhuqingfang
/// @Date 2022/10/8 1:55 下午
/// @Description native ad
class NativeAd extends Ad{
  final AdLoadCallback<NativeAd> adLoadCallback;
  ContentCallback<NativeAd>? contentCallback;
  NativeAd._(
      {required String placementId,
        required this.adLoadCallback})
      : super(placementId: placementId);
  static load(
      {required String placementId,
        required double width,
        required double height,
        required AdLoadCallback<NativeAd> adLoadCallback}) {
    NativeAd nativeAd = NativeAd._(
        placementId: placementId, adLoadCallback: adLoadCallback);
    SourceAdType source;
    switch(AdGlobalParamsManager.instance.getAdSource()){

      case AdSource.topOn:
        source = SourceAdType.topOnNativeAd;
        break;
      case AdSource.google:
        source = SourceAdType.adMobNativeAd;
        break;
      default:
        source = SourceAdType.topOnNativeAd;
        break;
    }
    nativeAd.loadConfig(source);
    nativeAd.thirdAdParams = ThirdAdParams(
        appId: AdGlobalParamsManager.instance.getAdAppId(),
        appKey: AdGlobalParamsManager.instance.getAdAppKey(),
        placementId: placementId,
        width: width,
        height: height,
        thirdAdEventCallback:
            (AdCallbackEvent adCallbackEvent, String message,Object extraInfo) {
          debugPrint("ad callback $adCallbackEvent message:$message");
          switch(adCallbackEvent) {
            case AdCallbackEvent.adLoaded:
              nativeAd.adLoadCallback.onAdLoaded(nativeAd);
              break;
            case AdCallbackEvent.adLoadFail:
              LoadAdError loadAdError = LoadAdError(-1, message);
              nativeAd.adLoadCallback.onAdFailLoad(nativeAd,loadAdError);
              break;
            case AdCallbackEvent.adOnShow:
              nativeAd.contentCallback?.onAdShow(nativeAd);
              break;
            case AdCallbackEvent.adOnDismissed:
              nativeAd.contentCallback?.onAdDismiss(nativeAd);
              break;
            case AdCallbackEvent.adOnClicked:
              nativeAd.contentCallback?.onAdClick(nativeAd);
              break;
            default:
              LoadAdError loadAdError = LoadAdError(-1,message);
              nativeAd.adLoadCallback.onAdFailLoad(nativeAd,loadAdError);
              break;
          }
        });
    if (thirdAdInstance.adSourceMap.containsKey(nativeAd.sourceType)) {
      nativeAd.thirdAd = thirdAdInstance
          .adSourceMap[nativeAd.sourceType](nativeAd.thirdAdParams);
      nativeAd.thirdAd.load();
    }
  }
}